package com.faceCaptor.main;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.faceCaptor.common.Result;
import com.faceCaptor.util.HttpUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginDialog {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private static Logger logger = LoggerFactory.getLogger(LoginDialog.class);
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginDialog window = new LoginDialog();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(571, 536);
		shell.setText("门店系统");
		
		Label label = new Label(shell, SWT.NONE);
		label.setAlignment(SWT.RIGHT);
		label.setBounds(99, 108, 90, 24);
		label.setText("门店编号：");
		
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(99, 192, 90, 24);
		label_1.setText("设备编号：");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(212, 105, 198, 30);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(212, 192, 198, 30);
		
		Button button = new Button(shell, SWT.NONE);
		button.setBounds(234, 292, 114, 34);
		button.setText("登录");
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String shopId = text.getText().trim();
				String deviceId = text_1.getText().trim();
				if (shopId == null || shopId.equals("") || deviceId == "" || deviceId.equals("")) {
					// MessageBox
					MessageDialog.openError(shell, "提示框", "门店编号和设备编号不能为空");
					return;
				
				}
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("shopId", shopId);
				jsonObject.put("deviceId", deviceId);
				String shopStr="";

				try{
					shopStr = HttpUtil.sendPostRequest(HttpUtil.HOST + HttpUtil.SHOPLOGIN, jsonObject.toJSONString());

				}catch(Exception exp){
					exp.printStackTrace();
				}
				if(shopStr.equals("")||shopStr==null){
					MessageDialog.openError(shell, "提示框", "网络连接出错");
					return;
				}
				Result result =JSON.parseObject(shopStr, Result.class);
				if(result!=null && result.getCode().equals("100")){
					//跳转到mainView
					shell.close();
					MainView.run(shopId);

				}else{
					MessageDialog.openError(shell, "提示框", "门店编号或者设备编号错误");
					return;
				}

			}
		});
		
		

	}
}
